package com.base_data_handler.surfer;

import java.io.File;
import java.util.Date;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
 * ClassName:surferFillContour
 * Description:surfer区域填充
 * Writer: XY
 * Create Date:2017年06月08日 上午15:26:23 
 * Rewriter: 
 * Rewrite Date:
 * Rewrite remark:
 * Version:
 */
public class surferFillContour 
{
	private ActiveXComponent surferComponent;
	private Dispatch surferObject;// surfer对象
	private Dispatch surferDocument; // surfer文档
	private String fileLeftName; //图片命名规则
	private String pngFileName ;//产生的图片名 fileLeftName+".png"
	String filepath="F:\\surfer绘图\\绘图调用文件\\宝鸡";
	public String getWarningPic(String fileSavePath,String datName, String blnName, String MapName, int picWidth, int PicHeight,String FillContour1,String FillContour2,String FillContour3,String FillContour4)
	{
	      try
	      {
	    	  	surferComponent = new ActiveXComponent("Surfer.Application");  //打开surfer软件
				surferObject = surferComponent.getObject();
				Dispatch.put(surferObject, "Visible", new Variant(false)); //软件是否可见
				Dispatch surferDocuments = surferComponent.getProperty("Documents").toDispatch(); // surfer文档集合
				java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmssSSS");
				fileLeftName = format2.format(new Date());
				surferDocument = Dispatch.call(surferDocuments, "Add",SrfDocTypes.srfDocPlot).toDispatch(); // 新建一个工作场景plot 
				Dispatch shapes = Dispatch.get(surferDocument, "Shapes").toDispatch();
				Dispatch countryframe0=Dispatch.call(shapes, "AddBaseMap",filepath + "\\" + blnName, "Defaults=1").toDispatch() ; //添加边界文件
				double Left= Dispatch.get(countryframe0, "Left").getDouble();
		    	double Top= Dispatch.get(countryframe0, "Top").getDouble();
		    	double Height= Dispatch.get(countryframe0, "Height").getDouble();
		    	double Width=Dispatch.get(countryframe0, "Width").getDouble();
				//Ⅰ级 红
				if(FillContour1!=null)
				{
					String[] Fcfilename1=FillContour1.split(",");
					for(String fc:Fcfilename1)
					{
						Dispatch bln=Dispatch.call(shapes, "AddBaseMap",filepath + "\\" + fc + ".bln", "").toDispatch() ;
						Dispatch base=Dispatch.call(Dispatch.get(bln, "Overlays").toDispatch() , "Item",1).toDispatch() ;
						Dispatch Fill = Dispatch.get(base, "Fill").toDispatch();
						Dispatch ForeColorRGBA = Dispatch.get(Fill, "ForeColorRGBA").toDispatch();
						Dispatch.put(ForeColorRGBA, "Red", 255);
						Dispatch.put(ForeColorRGBA, "Green", 0);
						Dispatch.put(ForeColorRGBA, "Blue", 50);
						Dispatch.put(ForeColorRGBA, "Alpha", 255);
						Dispatch.put(Fill, "Pattern", "Solid");
					}
				}
				//Ⅱ级 橙
				if(FillContour2!=null)
				{
					String[] Fcfilename2=FillContour2.split(",");
					for(String fc:Fcfilename2)
					{
						Dispatch bln=Dispatch.call(shapes, "AddBaseMap",filepath + "\\" + fc + ".bln", "").toDispatch() ;
						Dispatch base=Dispatch.call(Dispatch.get(bln, "Overlays").toDispatch() , "Item",1).toDispatch() ;
						Dispatch Fill = Dispatch.get(base, "Fill").toDispatch();
						Dispatch ForeColorRGBA = Dispatch.get(Fill, "ForeColorRGBA").toDispatch();
						Dispatch.put(ForeColorRGBA, "Red", 255);
						Dispatch.put(ForeColorRGBA, "Green", 150);
						Dispatch.put(ForeColorRGBA, "Blue", 0);
						Dispatch.put(ForeColorRGBA, "Alpha", 255);
						Dispatch.put(Fill, "Pattern", "Solid");
					}
				}
				 //Ⅲ级  黄
				if(FillContour3!=null)
				{
					String[] Fcfilename3=FillContour3.split(",");
					for(String fc:Fcfilename3)
					{
						Dispatch bln=Dispatch.call(shapes, "AddBaseMap",filepath + "\\" + fc + ".bln", "").toDispatch() ;
						Dispatch base=Dispatch.call(Dispatch.get(bln, "Overlays").toDispatch() , "Item",1).toDispatch() ;
						Dispatch Fill = Dispatch.get(base, "Fill").toDispatch();
						Dispatch ForeColorRGBA = Dispatch.get(Fill, "ForeColorRGBA").toDispatch();
						Dispatch.put(ForeColorRGBA, "Red", 255);
						Dispatch.put(ForeColorRGBA, "Green", 255);
						Dispatch.put(ForeColorRGBA, "Blue", 0);
						Dispatch.put(ForeColorRGBA, "Alpha", 255);
						Dispatch.put(Fill, "Pattern", "Solid");
					}
				}
				//Ⅳ级 蓝
				if(FillContour4!=null)
				{
					String[] Fcfilename4=FillContour4.split(",");
					for(String fc:Fcfilename4)
					{
						Dispatch bln=Dispatch.call(shapes, "AddBaseMap",filepath + "\\" + fc + ".bln", "").toDispatch() ;
						Dispatch base=Dispatch.call(Dispatch.get(bln, "Overlays").toDispatch() , "Item",1).toDispatch() ;
						Dispatch Fill = Dispatch.get(base, "Fill").toDispatch();
						Dispatch ForeColorRGBA = Dispatch.get(Fill, "ForeColorRGBA").toDispatch();
						Dispatch.put(ForeColorRGBA, "Red", 0);
						Dispatch.put(ForeColorRGBA, "Green", 150);
						Dispatch.put(ForeColorRGBA, "Blue", 255);
						Dispatch.put(ForeColorRGBA, "Alpha", 255);
						Dispatch.put(Fill, "Pattern", "Solid");
					}
				}
				//添加站点信息标注
				if(datName != "")
				{
					Dispatch Post=Dispatch.call(shapes,"AddPostMap",filepath + "\\" + datName,1,2,3,0,0).toDispatch() ;
					Dispatch PostMap=Dispatch.call(Dispatch.get(Post, "Overlays").toDispatch() , "Item",1).toDispatch() ;
					Dispatch.put(PostMap, "LabelPos", SrfPostPosType.srfPostPosBelow); //设置站点名显示在点下方
					Dispatch LabelFont = Dispatch.get(PostMap, "LabelFont").toDispatch();//设置站点名字体大小
					Dispatch.put(LabelFont, "Face", "宋体");
					Dispatch.put(LabelFont, "Size", 9);
					Dispatch Symbol = Dispatch.get(PostMap, "Symbol").toDispatch(); //设置标注点形状、颜色、大小
					Dispatch.put(Symbol, "Index", 12);
					Dispatch.put(Symbol, "Color", SrfColor.srfColorOrange);
					Dispatch.put(Symbol, "Size", 0.04);
				}
				//合并图层
				Dispatch win= Dispatch.call(surferObject, "Windows",1).toDispatch();
			    Dispatch.call(win,"Redraw");	//刷新屏幕
				Dispatch.call(shapes,"SelectAll");	
				Dispatch selected=  Dispatch.get(surferDocument, "Selection").toDispatch();// 返回所有选中对象
			    Dispatch.call(selected, "OverlayMaps");//将图层合并
	            //添加图例
			    if (MapName != "")
	            {
			    	Dispatch MapL=Dispatch.call(shapes, "AddBaseMap",filepath + "\\" + MapName, "Defaults=1").toDispatch() ;
			    	Dispatch.put(MapL, "Selected", true);
			    	Dispatch.put(MapL, "Width", 1.6);
			    	Dispatch.put(MapL, "Height", 1.4);
			    	Dispatch.put(MapL, "Left", Left+ 0.28);
			    	Dispatch.put(MapL, "Top", Top-Height+1.6);
			    	Dispatch AxesMapL = Dispatch.get(MapL, "Axes").toDispatch();
			    	Dispatch item1= Dispatch.call(AxesMapL, "Item",1).toDispatch();
			    	Dispatch item2= Dispatch.call(AxesMapL, "Item",2).toDispatch();
			    	Dispatch item3= Dispatch.call(AxesMapL, "Item",3).toDispatch();
			    	Dispatch item4= Dispatch.call(AxesMapL, "Item",4).toDispatch();
			    	Dispatch.put(item1, "MajorTickType", new Variant(SrfTickType.srfTickNone)); //坐标在轴线上显示方式
			    	Dispatch.put(item1, "ShowLabels", false);
			    	Dispatch.put(item2, "MajorTickType", new Variant(SrfTickType.srfTickNone));
			    	Dispatch.put(item3, "MajorTickType", new Variant(SrfTickType.srfTickNone));
			    	Dispatch.put(item3, "ShowLabels", false);
			    	Dispatch.put(item4, "MajorTickType", new Variant(SrfTickType.srfTickNone));
	            }
			    String strWH = "Width = " + picWidth + ", Height = " + PicHeight + " ,ColorDepth = 32";// 设置输出图片的高度和宽度
			    pngFileName = fileLeftName + ".png";
				Dispatch.call(surferDocument, "Export", fileSavePath + pngFileName,new Variant(false), strWH); //导出为固定格式png文件
				Dispatch.call(surferDocument, "Close", SrfSaveTypes.srfSaveChangesNo); //不生成srf文件
				quit();
				clearData(fileSavePath);
				return fileSavePath+pngFileName;
	      }
	      catch(Exception e)
	      {
	    	  e.printStackTrace();
			ComThread.Release();
			return "该要素的数据无法完成绘图";
	      }
	}
	/**删除绘图临时文件**/
	private void clearData(String fileSavePath)
	{
		File file = new File(fileSavePath+pngFileName+".gsr2");
		if (file.exists()) {
			file.delete();
		}
	}
	/** 退出程序**/
	public void quit() 
	{
		if (null == surferObject)
			return;
		if (null != surferDocument){
			Dispatch.call(surferObject, "Quit");
			ComThread.Release();
		}
	}
}
